import pandas as pd
import seaborn as sns
import os
import numpy as np
import itertools
from matplotlib import pyplot as plt
from wordcloud import WordCloud,STOPWORDS
import networkx as nx
# print(os.listdir("/input"))

matches = pd.read_csv(r"Kaggle/FIFAWorldCup/input/WorldCupMatches.csv")
players = pd.read_csv(r"Kaggle/FIFAWorldCup/input/WorldCupPlayers.csv")
cups = pd.read_csv(r"Kaggle/FIFAWorldCup/input/WorldCups.csv")
matches.isnull().sum()
sns.set_style("darkgrid")
matches = matches.drop_duplicates(subset="MatchID",keep="first")
matches = matches[matches["Year"].notnull()]


# overview
print("MATCHES-DATA")
print(matches.head(3))
print("PLAYERS-DATA")
print(players.head(3))
print("CUPS-DATA")
print(cups.head(3))

#total attendance of world cup every cup

att = matches.groupby("Year")["Attendance"].sum().reset_index()
att["Year"] = att["Year"].astype(int)
plt.figure(figsize=(12,7))
sns.barplot(att["Year"],att["Attendance"],linewidth=1,edgecolor="k"*len(att)*2)
plt.grid(True)
plt.title("Attendance by year",color='b')
plt.show()

# average attendance by year
att1 = matches.groupby("Year")["Attendance"].mean().reset_index()
att1["Year"] = att1["Year"].astype(int)
plt.figure(figsize=(12,7))
ax = sns.pointplot(att1["Year"],att1["Attendance"],color='r')
ax.set_facecolor("k")
plt.grid(True,color="green",alpha=.3)
plt.title("Average attendance by year",color='b')
plt.show()

#total goal scored by year
plt.figure(figsize=(13,7))
cups["Year1"] = cups["Year"].astype(str)
ax1 = plt.scatter("Year1","GoalsScored",data=cups,c=cups["GoalsScored"],cmap="inferno",
                 s=900,alpha=0.7,
                 linewidth=2,edgecolor="k",)

plt.xticks(cups["Year1"].unique())
plt.yticks(np.arange(60,200,20))
plt.title("Total goals Scored by year",color='b')
plt.show()

#total goals scored by year with point plot
plt.figure(figsize=(13,7))
ax1 = sns.pointplot(cups["Year"],cups["GoalsScored"],color='w')
ax1.set_facecolor("k")
plt.grid(True,color="grey",alpha=.3)
plt.title("Total goals scored by year",color='b')
plt.show()

#total matches played and qualified teams by year

plt.figure(figsize=(12,7))
sns.barplot(cups["Year"],cups["MatchesPlayed"],linewidth=1,edgecolor="k",color="b",label="Total matches played")
sns.barplot(cups["Year"],cups["QualifiedTeams"],linewidth=1,edgecolor="k",color="r",label="Total qualified teams")
plt.legend(loc="best",prop={"size":13})
plt.title("Qualified teams by year",color='b')
plt.grid(True)
plt.show()

#matches with highest number of attendance
h_att = matches.sort_values(by="Attendance",ascending=False)[:10]
h_att = h_att[['Year', 'Datetime','Stadium', 'City', 'Home Team Name',
              'Home Team Goals', 'Away Team Goals', 'Away Team Name', 'Attendance', 'MatchID']]
h_att["Stadium"] = h_att["Stadium"].replace('Maracan� - Est�dio Jornalista M�rio Filho',"Maracanã Stadium")
h_att["mt"] = h_att["Home Team Name"] + ".VS."+h_att["Away Team Name"]
plt.figure(figsize=(10,9))
ax = sns.barplot(y=h_att["mt"],x=h_att["Attendance"],palette="Spectral",linewidth=1,edgecolor="k")
plt.ylabel("teams")
plt.xlabel("Attendance")
plt.title("Matches with highest number of attendance",color='b')
plt.grid(True)
for i,j in enumerate(" stadium :"+h_att["Stadium"]+"Date :"+h_att["Datetime"]):
    ax.text(.7,i,j,fontsize=12,color="white",weight="bold")
plt.show()


#Stadiums with highest average attendance
matches["Year"] = matches["Year"].astype(int)
matches["Datetime"] = matches["Datetime"].str.split("-").str[0]
matches["Stadium"] = matches["Stadium"].str.replace('Estadio do Maracana',"Maracanã Stadium")
matches["Stadium"] = matches["Stadium"].str.replace('Maracan� - Est�dio Jornalista M�rio Filho',"Maracanã Stadium")
s_att = matches.groupby(["Stadium","City"]).mean().reset_index().sort_values(by="Attendance",ascending=False)[:15]
plt.figure(figsize=(8,9))
ax = sns.barplot(y=s_att["Stadium"],x=s_att["Attendance"],palette="cool",linewidth=1,edgecolor="k"*15)
plt.grid(True)
for i,j in enumerate(" City :"+s_att["City"]):
    ax.text(2.1,i,j,fontsize=14)
plt.title("Stadiums with highest average attendance",color="b")
plt.show()

#Cities that hosted highest world cup matches
# c_att = matches[["City","Year"]]
# c_att["Country"] = cups[cups["Year"]==c_att["Year"].astype(int)]
c_att = matches["City"].value_counts().reset_index()
# c_att = c_att.groupby("City").sum().reset_index()
c_att["Year"] = matches["Year"]

plt.figure(figsize=(10,8))
ax = sns.barplot(y=c_att["index"][:15],x=c_att["City"][:15],palette="plasma",
                 linewidth=1,edgecolor="k")
plt.xlabel("number of matches")
plt.ylabel("City")
plt.grid(True)
plt.title("Cities with maximum world cup matches",color='b')
for i,j in enumerate("Matches:"+c_att["City"][:15].astype(str)):
    ax.text(.7,i,j,fontsize=13,color="w")
plt.show()

#average attendance by city
aabc = matches.groupby("City")["Attendance"].mean().astype(int).reset_index()
aabc = aabc.sort_values(by="Attendance",ascending=False)
plt.figure(figsize=(10,8))
ax = sns.barplot(y=aabc["City"][:15],x=aabc["Attendance"][:15],linewidth=1,edgecolor="k",palette="Spectral_r")
# another format
'''
ax = sns.barplot("Attendance","City",
            data=ct_at[:20],
            linewidth = 1,
            edgecolor = "k"*20,
            palette  = "Spectral_r")
for i,j in enumerate(" Average attendance  : "+np.around(ct_at["Attendance"][:20],0).astype(str)):
    ax.text(.7,i,j,fontsize=12)
plt.grid(True)'''
for i,j in enumerate("Average Attendance :"+aabc["Attendance"].astype(str)):
    ax.text(0.7,i,j,fontsize=13,color="w")
plt.xlabel("Attendance")
plt.ylabel("City")
plt.title("Average Attendance by city")
plt.grid(True)
plt.show()

#Teams with the most world cup final victories
cups["Year1"] = cups["Year"]
cups["Winner"]=cups["Winner"].replace("Germany FR","Germany")
cups["Runners-Up"]=cups["Runners-Up"].replace("Germany FR","Germany")
cups["Year"] = cups["Year"].astype(str)

#note :use apply and join to connect the winner years
c1  = cups .groupby("Winner")["Year"].apply(" , ".join).reset_index()
c2 = cups.groupby("Winner")["Year"].count().reset_index()
c12 = c1.merge(c2,left_on="Winner",right_on="Winner",how="left")
c12 = c12.sort_values(by="Year_y",ascending=False)

plt.figure(figsize=(10,8))
ax = sns.barplot("Year_y","Winner",data=c12,
                 palette="jet_r",
                 alpha=.8,
                 linewidth=2,
                 edgecolor="k")

for i,j in enumerate("Years :"+ c12["Year_x"]):
    ax.text(.1,i,j,weight="bold")

plt.title("Teams with the most world cup final victories")
plt.grid(True)
plt.xlabel("count")
plt.show()

#world cup final results by nation
cou = cups["Winner"].value_counts().reset_index()
cou_w = cou.copy()
cou_w.columns = ["Country","count"]
cou_w["type"] = "WINNER"

cou_r = cups["Runners-Up"].value_counts().reset_index()
cou_r.columns = ["Country","count"]
cou_r["type"] = "RUNNER - UP"
cou_t = pd.concat([cou_w,cou_r],axis=0)
plt.figure(figsize=(8,10))
#hue parameter to divide the barplot by group
sns.barplot("count","Country",data=cou_t,hue="type",palette=["lime","r"],
            linewidth=1,edgecolor="k")
plt.grid(True)
plt.legend(loc="center right",prop={"size":14})
plt.title("Final results by nation",color='b')
plt.show()

#Teams with the most world cup matches
matches["Home Team Name"] = matches["Home Team Name"].str.replace('rn">United Arab Emirates',"United Arab Emirates")
matches["Home Team Name"] = matches["Home Team Name"].str.replace("C�te d'Ivoire","Côte d’Ivoire")
matches["Home Team Name"] = matches["Home Team Name"].str.replace('rn">Republic of Ireland',"Republic of Ireland")
matches["Home Team Name"] = matches["Home Team Name"].str.replace('rn">Bosnia and Herzegovina',"Bosnia and Herzegovina")
matches["Home Team Name"] = matches["Home Team Name"].str.replace('rn">Serbia and Montenegro',"Serbia and Montenegro")
matches["Home Team Name"] = matches["Home Team Name"].str.replace('rn">Trinidad and Tobago',"Trinidad and Tobago")
matches["Home Team Name"] = matches["Home Team Name"].str.replace("Soviet Union","Russia")
matches["Home Team Name"] = matches["Home Team Name"].str.replace("Germany FR","Germany")

matches["Away Team Name"] = matches["Away Team Name"].str.replace('rn">United Arab Emirates',"United Arab Emirates")
matches["Away Team Name"] = matches["Away Team Name"].str.replace("C�te d'Ivoire","Côte d’Ivoire")
matches["Away Team Name"] = matches["Away Team Name"].str.replace('rn">Republic of Ireland',"Republic of Ireland")
matches["Away Team Name"] = matches["Away Team Name"].str.replace('rn">Bosnia and Herzegovina',"Bosnia and Herzegovina")
matches["Away Team Name"] = matches["Away Team Name"].str.replace('rn">Serbia and Montenegro',"Serbia and Montenegro")
matches["Away Team Name"] = matches["Away Team Name"].str.replace('rn">Trinidad and Tobago',"Trinidad and Tobago")
matches["Away Team Name"] = matches["Away Team Name"].str.replace("Germany FR","Germany")
matches["Away Team Name"] = matches["Away Team Name"].str.replace("Soviet Union","Russia")

ht = matches["Home Team Name"].value_counts().reset_index()
ht.columns = ['team','matches']
at = matches['Away Team Name'].value_counts().reset_index()
at.columns = ['team','matches']
mt = pd.concat([ht,at],axis=0)
mt = mt.groupby("team")["matches"].sum().reset_index().sort_values(by="matches",ascending=False)
plt.figure(figsize=(10,13))
ax = sns.barplot("matches","team",data=mt[:25],palette="gnuplot_r",
                 linewidth=1,edgecolor="k")
plt.grid(True)
plt.title("Team with the most matches",color="b")
for i,j in enumerate("Matches played :"+mt["matches"][:25].astype(str)):
    ax.text(.7,i,j,fontsize=12,color="white")
plt.show()

#Teams with the most tournament participations
hy = matches[["Year","Home Team Name"]]
hy.columns = ["year","team"]
hy["type"] = "HOME TEAM"
ay = matches[["Year","Away Team Name"]]
ay.columns = ["year","team"]
ay["type"] = "AWAY TEAM"

home_away = pd.concat([hy,ay],axis=0)
yt = home_away.groupby(["year","team"]).count().reset_index()
yt = yt["team"].value_counts().reset_index()
plt.figure(figsize=(10,8))
sns.barplot("team","index",data=yt[:15],linewidth=1,edgecolor="k")
for i,j in enumerate("Participated "+yt["team"][:15].astype(str)+" times"):
    ax.text(.7,i,j,fontsize=14,color="k")
plt.grid(True)
plt.title("Teams with the most tournaments participations")
plt.xlabel("count")
plt.ylabel("team")
# plt.xticks(np.arange(0,22,2)
plt.show()

#distribution of home and away goals
plt.figure(figsize=(12,13))
#the arguements below represent the y and x's divide,the last parameter represent the position of the plot
plt.subplot(211)
sns.distplot(matches["Home Team Goals"],color='b',rug=True)
plt.xticks(np.arange(0,12,1))
plt.title("Distribution of Home Team Goals",color='b')

plt.subplot(212)
sns.distplot(matches['Away Team Goals'],color='r',rug=True)
plt.xticks(np.arange(0,9,1))
plt.title("Distribution of Away Team Goals",color='b')
plt.show()


#distribution of half time home and away team goals
plt.figure(figsize=(12,15))
matches = matches.rename(columns={'Half-time Home Goals':"first half home goals",
                                  'Half-time Away Goals':"first half away goals"})
matches["second half home goals"] = matches["Home Team Goals"] - matches["first half home goals"]
matches["second half away goals"] = matches["Away Team Goals"] - matches["first half away goals"]

plt.subplot(211)
sns.kdeplot(matches["first half home goals"],color="b",linewidth=2)
sns.kdeplot(matches["second half home goals"],color='r',linewidth=2)
plt.xticks(np.arange(0,9,1))
plt.title("Distribution of first and second Half - Home Team Goals",color='b')

plt.subplot(212)
sns.kdeplot(matches["first half away goals"],color='b',linewidth=2)
sns.kdeplot(matches["second half away goals"],color='r',linewidth=2)
plt.xticks(np.arange(0,9,1))
plt.title("Distribution of first and second Half - Away Home Goals",color='b')
plt.show()

#home and away goals by year
gh = matches[['Year','Home Team Goals']]
gh.columns = ["Year","goals"]
gh['type'] = "Home Team Goals"

ga = matches[["Year","Away Team Goals"]]
ga.columns = ["Year","goals"]
ga['type'] = "Away Team Goals"

gls = pd.concat([ga,gh],axis=0)
plt.figure(figsize=(13,8))
sns.violinplot(gls["Year"],gls["goals"],hue=gls["type"],split=True,inner="quart",palette="husl")
plt.grid(True)
plt.title("Home and away goals by year",color='b')
plt.show()

#kdeplot test
x = np.random.randn(100)
y = np.random.randn(100)
sns.kdeplot(x,y,shade=True)
plt.show()

#first half home and away goals by year
hhg = matches[["Year","first half home goals"]]
hhg.columns = ['year','goals']
hhg['type'] = 'first half home goals'

hag = matches[['Year','first half away goals']]
hag.columns = ['year','goals']
hag['type'] = 'first half away goals'

h_time = pd.concat([hhg,hag],axis=0)
plt.figure(figsize=(13,8))
sns.violinplot(h_time['year'],h_time['goals'],hue=gls['type'],split=True,inner='quart',palette='gist_ncar')
plt.grid(True)
plt.title("firsts half home and away goals by year",color='b')
plt.show()

#second half home and away goals by year
shg = matches[["Year","second half home goals"]]
shg.columns = ['year','goals']
shg['type'] = 'second half home goals'

sag = matches[['Year','second half away goals']]
sag.columns = ['year','goals']
sag['type'] = 'second half away goals'

h_time = pd.concat([shg,sag],axis=0)
plt.figure(figsize=(13,8))
sns.violinplot(h_time['year'],h_time['goals'],hue=gls['type'],split=True,inner='quart',palette='gist_ncar')
plt.grid(True)
plt.title("second half home and away goals by year",color='b')
plt.show()

#matches outcomes by home and away teams
def label(matches):
    if matches['Home Team Goals'] > matches['Away Team Goals']:
        return "Home Team win"
    elif matches['Home Team Goals'] < matches['Away Team Goals']:
        return "Away Team win"
    else:
        return "DRAW"

matches['outcome'] = matches.apply(lambda matches:label(matches),axis=1)
plt.figure(figsize=(9,9))
matches['outcome'].value_counts().plot.pie(autopct="%1.0f%%",fontsize=14,colors=sns.color_palette("husl"),wedgeprops={"linewidth":2,"edgecolor":"white"})
circ = plt.Circle((0,0),.7,color="white")
plt.gca().add_artist(circ)
plt.title("# Match outcomes by home and away teams",color='b')
plt.show()

#matches outcomes by countries
#matches[['Home Team Name', 'Home Team Goals', 'Away Team Goals', 'Away Team Name', "outcome"]]


def win_label(matches):
    if matches["Home Team Goals"] > matches["Away Team Goals"]:
        return matches["Home Team Name"]
    if matches["Home Team Goals"] < matches["Away Team Goals"]:
        return matches["Away Team Name"]
    if matches["Home Team Goals"] == matches["Away Team Goals"]:
        return "DRAW"


def lst_label(matches):
    if matches["Home Team Goals"] < matches["Away Team Goals"]:
        return matches["Home Team Name"]
    if matches["Home Team Goals"] > matches["Away Team Goals"]:
        return matches["Away Team Name"]
    if matches["Home Team Goals"] == matches["Away Team Goals"]:
        return "DRAW"

matches['win_team'] = matches.apply(lambda matches:win_label(matches),axis=1)
matches['lost_team'] = matches.apply(lambda matches:lst_label(matches),axis=1)
lst = matches['lost_team'].value_counts().reset_index()
win = matches['win_team'].value_counts().reset_index()
wl = win.merge(lst,left_on='index',right_on='index',how='left')
wl = wl[wl['index']!="DRAW"]
wl.columns = ['team','wins','loses']
#copy the dataframe
wll = wl.copy()
wll = wll.merge(mt,left_on="team",right_on='team',how='left')
wll['draws'] = wll['matches'] - (wll['wins']+wll['loses'])
#set the index
wll.index = wll.team
wll[['wins','draws','loses']].plot(kind='barh',stacked=True,
                                  figsize=(10,17),color=['lawngreen','royalblue','r'],
                                  linewidth=1,edgecolor='k')
plt.legend(loc='center right',prop={"size":20})
plt.xticks(np.arange(0,120,5))
plt.title("Matches outcomes by countries",color='b')
plt.show()

#team with highest FIFA world cup goals
tt_gl_h = matches.groupby("Home Team Name")["Home Team Goals"].sum().reset_index()
tt_gl_h.columns = ["team","goals"]

tt_gl_a = matches.groupby("Away Team Name")["Away Team Goals"].sum().reset_index()
tt_gl_a.columns = ["team","goals"]

total_goals = pd.concat([tt_gl_h,tt_gl_a],axis=0)
total_goals = total_goals.groupby("team")["goals"].sum().reset_index()
total_goals = total_goals.sort_values(by="goals",ascending =False)
total_goals["goals"] = total_goals["goals"].astype(int)

plt.figure(figsize=(10,12))
ax= sns.barplot("goals","team",data=total_goals[:20],palette="cool",
                linewidth=1,edgecolor="k"*20)

for i,j in enumerate("SCORED  " +total_goals["goals"][:20].astype(str) + "  GOALS"):
    ax.text(.7,i,j,fontsize = 10,color="k")

plt.title("Teams with highest fifa world cup goals",color='b')
plt.grid(True)
plt.show()

#highest total goals scored during a match
matches['total_goals'] = matches['Home Team Goals'] + matches['Away Team Goals']

#total goals scores during games by year
plt.figure(figsize=(13,8))
sns.boxplot(y=matches['total_goals'],x=matches['Year'])
plt.grid(True)
plt.title("Total gaols scored during game by year",color='b')
plt.show()

###################################################################
#Team comparator
matches_played = mt.copy()
mat_new = matches_played.merge(lst,left_on='team',right_on='index',how='left')
mat_new = mat_new.merge(win,left_on='team',right_on='index',how='left')
mat_new = mat_new[['team','matches','lost_team','win_team']]
mat_new = mat_new.fillna(0)
mat_new['win_team'] = mat_new['win_team'].astype(int)
mat_new['draws'] = mat_new['matches']-(mat_new['lost_team']+mat_new["win_team"])
mat_new = mat_new.merge(total_goals,left_on='team',right_on='team',how='left')
mat_new = mat_new.rename(columns={'win':'wins','lost_team':'loses'})

def team_compare(team1,team2):
    lst = [team1,team2]
    dat = mat_new[mat_new['team'].isin(lst)]
    plt.figure(figsize=(12,10))
    cols = ['matches','goals','wins','loses','draws']
    length = len(cols)
    for i,j in itertools.zip_longest(cols,range(length)):
        fig = plt.subplot(3,2,j+1)
        ax = sns.barplot(dat[i],dat['team'],palette=['royalblue','r'],linewidth=2,edgecolor='k')
        plt.ylabel("")
        plt.yticks(fontsize=13)
        plt.grid(True,color='grey',alpha=.3)
        plt.title(i,color='b',fontsize=15)
        plt.subplots_adjust(wspace=.3,hspace=.5)
        fig.set_facecolor("w")
        for k,l in enumerate(dat[i].values):
            ax.text(.7,k,l,weight='bold',fontsize=20)

###################################################################
#interaction between teams
def interactions(year,color):
    df = matches[matches["Year"]==year][["Home Team Name","Away Team Name"]]
    G = nx.from_pandas_edgelist(df,"Home Team Name","Away Team Name")
    plt.figure(figsize=(10,9))
    nx.draw_kamada_kawai(G,with_labels=True,
                         node_size = 2500,
                         node_color = color,
                         node_shape = "h",
                         edgecolor='k',
                         linewidths=5,
                         font_size=13,
                         alpha=.8)
    plt.title("Interaction between teams :"+str(year),fontsize=13,color='b')

###################################################################
#wordcloud:player names
wrds = players['Player Name'].value_counts().keys()
wc = WordCloud(scale=5,max_words=1000,colormap='rainbow').generate("".join(wrds))
plt.figure(figsize=(13,15))
plt.imshow(wc,interpolation="bilinear")
plt.axis("off")
plt.title("Player names - word cloud",color='b')
plt.show()

wrds1 = players['Coach Name'].value_counts().keys()
wc1 = WordCloud(scale=5,max_words=1000,colormap='rainbow',background_color="black").generate(" ".join(wrds1))
plt.figure(figsize=(13,14))
plt.imshow(wc1,interpolation="bilinear")
plt.axis("off")
plt.title("Coach Name - word cloud",color='b')
plt.show()

###################################################################
#The Golden Goal
win_conditions = matches[(matches["Win conditions"] != " ") & (matches['Win conditions'].notnull())]
extra_time = win_conditions[win_conditions['Win conditions'].str.contains('extra time')]
penalties = win_conditions[win_conditions['Win conditions'].str.contains('penalties')]
gold_goals = win_conditions[win_conditions['Win conditions'].str.contains("Golden Goal")]
gold_goals.index = gold_goals['MatchID'].astype(int)
plt.figure(figsize=(13,14))
gg = gold_goals[["Datetime","Stage","Home Team Name",
              "Away Team Name","Home Team Goals",
              "Away Team Goals","Win conditions"]].transpose()
gg.style.set_properties(**{"background-color":"black",
                           "color":"white",
                           "border-color":"lawngreen"}).set_caption("Matches with Golden Goals")
plt.show()

##########################################################################################################
#Match outcomes for countries playing 2018 quarter finals
cou = ["Uruguay","France",
       "Brazil" , "Belgium",
       "Sweden" , "England",
       "Russia" , "Croatia" ]

length = len(cou)
qtr = mat_new[mat_new['team'].isin(cou)]
plt.figure(figsize=(12,25))

for i,j in itertools.zip_longest(cou,range(length)):
    plt.subplot(4,2,j+1)

    lab = ['win_team','draws','loses']
    plt.pie(qtr[qtr['team']==i][lab].values.tolist()[0],
            labels = ['win_team','draws','loses'],
            autopct = "%1.0f%%",
            shadow = True,
            wedgeprops = {'linewidth':3.5,'edgecolor':"white"},
            colors = {'lawngreen','royalblue','tomato'})
    plt.axis('equal')
    plt.legend()
    circ = plt.Circle((0,0),.7,color='white')
    plt.gca().add_artist(circ)
    plt.title(i,color='navy',fontsize=14)




